import { useState, useCallback } from 'react';

    /**
     * 自定义Hook，用于管理通知消息
     * @returns {Object} 包含通知状态和方法的对象
     */
    const useNotification = () => {
      const [notifications, setNotifications] = useState([]);

      /**
       * 添加通知
       * @param {string} message - 通知消息
       * @param {string} type - 通知类型 ('success', 'error', 'warning', 'info')
       * @param {number} duration - 显示时长（毫秒），设为0则不自动关闭
       */
      const addNotification = useCallback((message, type = 'info', duration = 3000) => {
        const id = Date.now() + Math.random().toString(36).substr(2, 9);
        
        setNotifications(prev => [...prev, { id, message, type, timestamp: Date.now() }]);
        
        // 如果设置了duration，则在指定时间后自动移除通知
        if (duration > 0) {
          setTimeout(() => {
            removeNotification(id);
          }, duration);
        }
        
        return id;
      }, []);

      /**
       * 移除通知
       * @param {string} id - 通知ID
       */
      const removeNotification = useCallback((id) => {
        setNotifications(prev => prev.filter(notification => notification.id !== id));
      }, []);

      /**
       * 清除所有通知
       */
      const clearAllNotifications = useCallback(() => {
        setNotifications([]);
      }, []);

      /**
       * 快捷方法：添加成功通知
       */
      const success = useCallback((message, duration) => {
        return addNotification(message, 'success', duration);
      }, [addNotification]);

      /**
       * 快捷方法：添加错误通知
       */
      const error = useCallback((message, duration) => {
        return addNotification(message, 'error', duration);
      }, [addNotification]);

      /**
       * 快捷方法：添加警告通知
       */
      const warning = useCallback((message, duration) => {
        return addNotification(message, 'warning', duration);
      }, [addNotification]);

      /**
       * 快捷方法：添加信息通知
       */
      const info = useCallback((message, duration) => {
        return addNotification(message, 'info', duration);
      }, [addNotification]);

      return {
        notifications,
        addNotification,
        removeNotification,
        clearAllNotifications,
        success,
        error,
        warning,
        info,
      };
    };

    export default useNotification;
